package com.treasure.service.third.handler;

import com.treasure.service.commons.lang.util.MD5Util;
import com.treasure.service.third.utils.TenpayHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 封装支付请求数据及发送
 * @ClassName: RestClient
 * @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
 * @date 2018年6月06日 下午10:42:11
 *
 */
public class PrepayIdRequestHandler extends RequestHandler {

    private Logger logger = LoggerFactory.getLogger(PrepayIdRequestHandler.class);

//    public PrepayIdRequestHandler(HttpServletRequest request,
//                                  HttpServletResponse response) {
//        super(request, response);
//    }
    public PrepayIdRequestHandler() {
        super();
    }

//    public String createMD5Sign() {
//        StringBuffer sb = new StringBuffer();
//        Set es = super.getAllParameters().entrySet();
//        Iterator it = es.iterator();
//
//        List<String> keys = new ArrayList<String>();
//        Map<String,String> map = new HashMap<>();
//        while (it.hasNext()) {
//            Map.Entry entry = (Map.Entry) it.next();
//            String k = (String) entry.getKey();
//            String v = (String) entry.getValue();
//            keys.add(k);
//            map.put(k,v);
//        }
//
//        keys.sort(String.CASE_INSENSITIVE_ORDER);
//
//        for(String key : keys){
//            sb.append(key + "=" + map.get(key) + "&");
//        }
//
//        String params=sb.substring(0);
//        String sign = MD5Util.MD5Encode(params, "utf8");
//        return sign.toUpperCase();
//    }

    public String createMD5Sign() {
        StringBuffer sb = new StringBuffer();
        Set es = super.getAllParameters().entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            sb.append(k + "=" + v + "&");
        }
//        String params=sb.substring(0)+"key=943ac4d4d2068b289e24bf4ede2ea94d";
        String params=sb.substring(0)+"key=dad6cbd4117025692ca1a21527336763";
        logger.info("调预支付待生成签名串：{}",params);
        String sign = MD5Util.MD5Encode(params, "utf8");
        return sign.toUpperCase();
    }

    public String getParameters(){
        String prepayid = "";
        Set es=super.getAllParameters().entrySet();
        Iterator it=es.iterator();
        StringBuffer sb = new StringBuffer("<xml>\n");
        while(it.hasNext()){
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            sb.append("<"+k+">"+v+"</"+k+">\n");
        }
        sb.append("</xml>");
        String params=sb.substring(0);

        return params;
    }

    // 提交预支付
    public String sendPrepay(String params) throws Exception {

        String requestUrl = super.getGateUrl();
        System.out.println("请求url："+requestUrl);
        TenpayHttpClient httpClient = new TenpayHttpClient();
        httpClient.setReqContent(requestUrl);
        String resContent = "";
        Map<String,String> map = new HashMap<>();
        if (httpClient.callHttpPost(requestUrl, params)) {
            resContent = httpClient.getResContent();
            System.out.println("获取prepayid的返回值："+resContent);
//            if(map.containsKey("prepay_id"))
//                prepayid=map.get("prepay_id");
        }else{
            map.put("return_code","FAIL");
            map.put("result_code","FAIL");
            map.put("return_msg","调用微信支付失败");
        }
        return resContent;
    }
}
